package com.hcrazy.manchester.app.services;

import java.util.HashMap;
import java.util.Map;

import org.joda.time.DateTime;

import com.hcrazy.manchester.app.jdbc.BookFoodDAO;
import com.hcrazy.manchester.app.jdbc.CommentDAO;
import com.hcrazy.manchester.app.jdbc.CouponDAO;
import com.hcrazy.manchester.app.jdbc.LiveAddressDAO;
import com.hcrazy.manchester.app.jdbc.RentDAO;
import com.hcrazy.manchester.app.jdbc.SecondaryDAO;
import com.hcrazy.manchester.app.pojo.Comment;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class CommentService {

	private CommentDAO commentDAO;
	
	public CommentDAO getCommentDAO() {
		return commentDAO;
	}

	public void setCommentDAO(CommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}
	
	private LiveAddressDAO liveAddressDAO;
	
	public LiveAddressDAO getLiveAddressDAO() {
		return liveAddressDAO;
	}

	public void setLiveAddressDAO(LiveAddressDAO liveAddressDAO) {
		this.liveAddressDAO = liveAddressDAO;
	}
	
	private BookFoodDAO bookFoodDAO;

	public BookFoodDAO getBookFoodDAO() {
		return bookFoodDAO;
	}

	public void setBookFoodDAO(BookFoodDAO bookFoodDAO) {
		this.bookFoodDAO = bookFoodDAO;
	}
	
	private CouponDAO couponDAO;

	public CouponDAO getCouponDAO() {
		return couponDAO;
	}

	public void setCouponDAO(CouponDAO couponDAO) {
		this.couponDAO = couponDAO;
	}

	private RentDAO rentDAO;
	
	public RentDAO getRentDAO() {
		return rentDAO;
	}

	public void setRentDAO(RentDAO rentDAO) {
		this.rentDAO = rentDAO;
	}
	
	private SecondaryDAO secondaryDAO;
	
	public SecondaryDAO getSecondaryDAO() {
		return secondaryDAO;
	}

	public void setSecondaryDAO(SecondaryDAO secondaryDAO) {
		this.secondaryDAO = secondaryDAO;
	}

	/**
	 * 发表或者修改评论
	 * @param comment
	 * @return
	 * @throws Exception
	 */
	public Object addOrUpdateComment(Comment comment) throws Exception{
		Map<String,String> result = new HashMap<String,String>();
		result.put(Constant.SINGLE_RETURN,"true");
		
		DateTime dt = new DateTime();
		comment.setCommentTime(dt.toString("yyyy-MM-dd HH:mm:ss"));
		
		if(comment.getCommentId()>0){
			commentDAO.updateComment(comment);
		}else{
			commentDAO.addComment(comment);
			//根据模块不同分别设置模块评论数量
			switch(comment.getModuleType()){
				case Constant.COMM_ADDRESS_INFO_MODULE:
					liveAddressDAO.addCommentCount(comment.getModuleId());
					break;
				case Constant.COMM_BOOK_INFO_MODULE:
					bookFoodDAO.addCommentCount(comment.getModuleId());
					break;
				case Constant.COMM_COUPON_INFO_MODULE:
					couponDAO.addCommentCount(comment.getModuleId());
					break;
				case Constant.COMM_RENT_INFO_MODULE:
					rentDAO.addCommentCount(comment.getModuleId());
					break;
				case Constant.COMM_SECONDARY_INFO_MODULE:
					secondaryDAO.addCommentCount(comment.getModuleId());
					break;
				case Constant.COMM_SIMPLE_INFO_MODULE:
					
					break;
				case Constant.COMM_TICKET_INFO_MODULE:
					bookFoodDAO.addCommentCount(comment.getModuleId());
					break;
				default:
					break;
			}
		}
		
		return result;
	}
	
	public Comment addOrUpdateCommentWithResult(Comment comment) throws Exception{
		
		long commentId = 0;
		
		DateTime dt = new DateTime();
		comment.setCommentTime(dt.toString("yyyy-MM-dd HH:mm:ss"));
		
		if(comment.getCommentId()>0){
			commentDAO.updateComment(comment);
			commentId = comment.getCommentId();
		}else{
			commentId = commentDAO.addCommentWithResult(comment);
			//根据模块不同分别设置模块评论数量
			switch(comment.getModuleType()){
				case Constant.COMM_ADDRESS_INFO_MODULE:
					liveAddressDAO.addCommentCount(comment.getModuleId());
					break;
				case Constant.COMM_BOOK_INFO_MODULE:
					bookFoodDAO.addCommentCount(comment.getModuleId());
					break;
				case Constant.COMM_COUPON_INFO_MODULE:
					couponDAO.addCommentCount(comment.getModuleId());
					break;
				case Constant.COMM_RENT_INFO_MODULE:
					rentDAO.addCommentCount(comment.getModuleId());
					break;
				case Constant.COMM_SECONDARY_INFO_MODULE:
					secondaryDAO.addCommentCount(comment.getModuleId());
					break;
				case Constant.COMM_SIMPLE_INFO_MODULE:
					
					break;
				case Constant.COMM_TICKET_INFO_MODULE:
					bookFoodDAO.addCommentCount(comment.getModuleId());
					break;
				default:
					break;
			}
		}
		
		return commentDAO.getById(commentId);
	}
	
	public PageModel getCommonInfos(PageModel model,int moduleType,long moduleId,long userId) throws Exception{
		return commentDAO.getPagedComments(model, moduleType,moduleId,userId);
	}
	
	public PageModel getCommonInfos(PageModel model,int moduleType,String title,String username) throws Exception{
		return commentDAO.getPagedComments(model, moduleType, title, username);
	}
	
	public Comment getInfoById(int infoId) throws Exception{
		return commentDAO.getById(infoId);
	}
	
	public void delComment(int infoId) throws Exception{
		commentDAO.delComment(infoId);
	}
	
	/**
	 * 获取模块被评论数量
	 * @param moduleType
	 * @param moduleId
	 * @return
	 * @throws Exception
	 */
	public long getCountByModule(int moduleType,long moduleId) throws Exception {
		long count = 0;
		
		switch(moduleType){
			case Constant.COMM_ADDRESS_INFO_MODULE:
				count = liveAddressDAO.getCommentCount(moduleId);
				break;
			case Constant.COMM_BOOK_INFO_MODULE:
				count = bookFoodDAO.getCommentCount(moduleId);
				break;
			case Constant.COMM_COUPON_INFO_MODULE:
				count = couponDAO.getCommentCount(moduleId);
				break;
			case Constant.COMM_RENT_INFO_MODULE:
				count = rentDAO.getCommentCount(moduleId);
				break;
			case Constant.COMM_SECONDARY_INFO_MODULE:
				count = secondaryDAO.getCommentCount(moduleId);
				break;
			case Constant.COMM_SIMPLE_INFO_MODULE:
				
				break;
			case Constant.COMM_TICKET_INFO_MODULE:
				count = bookFoodDAO.getCommentCount(moduleId);
				break;
			default:
				break;
		}
		
		return count;
	}
}
